package com.aistar.controller;

import com.aistar.pojo.User;
import com.aistar.service.UserService;
import com.aistar.util.Email;
import com.aistar.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/")
public class UserConller {

    @RequestMapping("toLogin")
    public String toLogin() {
        return "user/login";
    }

    @RequestMapping("toReg")
    public String toReg() {
        return "user/register";
    }

    @Controller
    @RequestMapping("/user")
    public class UserController {

        @Autowired(required = false)
        private UserService userService;

        @RequestMapping("/toReg")
        private String toReg() {
            return "user/register";
        }

        @RequestMapping("/toLogin")
        public String toLogin() {
            return "user/login";
        }

        @RequestMapping("/toResetPassword")
        public String toResetPassword() {
            return "user/password";
        }

        @RequestMapping("/checkEmail")
        @ResponseBody
        public ResponseResult checkEmail(String email) {
            cn.com.webxml.ValidateEmailWebService service = new cn.com.webxml.ValidateEmailWebService();
            cn.com.webxml.ValidateEmailWebServiceSoap port = service.getPort(cn.com.webxml.ValidateEmailWebServiceSoap.class);
            short i = port.validateEmailAddress(email);
            if (i != 1) {
                return new ResponseResult(201, "请输入正确的邮件");
            }
            return new ResponseResult(200, "邮箱正确");
        }

        @RequestMapping("/sendEmail")
        @ResponseBody
        public ResponseResult sendEmail(String email, HttpSession session) {
            System.out.println("==============>" + email);
            String result = Email.sendEmail(email);
            if (result == null) {
                return new ResponseResult(201, "发送失败");
            }
            System.out.println("==============>" + result);
            session.setAttribute("emailCode", result);
            return new ResponseResult(200, "发送成功", result);
        }


        @RequestMapping("/reg")
        @ResponseBody
        public ResponseResult insertUser(User user, String code, HttpSession session) {
            System.out.println("=======>"+user);
            ResponseResult responseResult = userService.reg(user, code, session);
            return responseResult;
        }

        @RequestMapping("/login")
        @ResponseBody
        public ResponseResult login(User user, String code, HttpSession session, HttpServletRequest request) {
            System.out.println(user);
            System.out.println(code);
            ResponseResult responseResult = userService.login(user, code, session,request);
            return responseResult;
        }

        @RequestMapping("/resetPassword")
        @ResponseBody
        public ResponseResult resetPassword(User user, String code,HttpSession session) {
            ResponseResult responseResult = userService.resetPassword(user, code,session);
            return responseResult;
        }

    }
}
